package com.baizhi.springmvc.controller;


import com.baizhi.springmvc.entity.Book;
import com.baizhi.springmvc.entity.Category;
import com.baizhi.springmvc.service.BookService;
import com.baizhi.springmvc.service.CategoryService;
import com.baizhi.springmvc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
public class BookController{

@Autowired
private BookService bookService;
@Autowired
private CategoryService categoryService;
@Autowired
private CommentService commentService;

//跳转主页面
    @RequestMapping("/main")
    public String main(Map map) {
        List categoryList = categoryService.selectAllFirst();
        List custome_scoreList = bookService.selectByCustome_score();
        List  groundingDateList = bookService.selectByGroundingDate();
        List inventoryList = bookService.selectByInventory();
        List salenumList = bookService.selectBySalenum();
        map.put("categoryList",categoryList);
        map.put("custome_scoreList",custome_scoreList);
        map.put("groundingDateList",groundingDateList);
        map.put("inventoryList",inventoryList);
        map.put("salenumList",salenumList);
        return "forward:/main/main.jsp";
    }
    //查询一级类别
    @RequestMapping("/selectCateByFirstLevel")
    public String selectCateByFirstLevel(@ModelAttribute("id")Integer id,Map map,Integer pageNum,@ModelAttribute("category_type")String category_type,@ModelAttribute("category_name")String category_name) {
        if (pageNum==null){
            pageNum=1;
        }
        Integer pageSize=5;
        //根据一级类别查询本类别下所有二级类别
        List categoryList = categoryService.selectCateByFirstLevel(id);
        //根据一级类别查询本类下所有二级类别下的所有图书
        List bookList = bookService.selectByFirstid(id,pageNum,pageSize);
        //总页数
        Integer totalPage = bookService.firstTotalPage(id, pageSize);
        map.put("categoryList",categoryList);
        map.put("bookList",bookList);
        map.put("totalPage",totalPage);
        map.put("pageNum", pageNum);
        return "forward:/main/book_list.jsp";
    }
//查询二级类别
    @RequestMapping("/selectCateByTwoLevel")
    public String selectCateByTwoLevel(@ModelAttribute("id")Integer id, Map map, Integer pageNum,@ModelAttribute("category_type")String category_type,@ModelAttribute("category_name")String category_name) {
        if (pageNum==null){
            pageNum=1;
        }
        Integer pageSize=5;
        //根据二级类别查询一级类别下所有类别
        List categoryList = categoryService.selectCateByTwoLevel(id);
        //查询二级类别下的图书
        List bookList = bookService.selectByTwoid(id,pageNum,pageSize);
        //总页数
        Integer totalPage = bookService.twoTotalPage(id, pageSize);
        //根据二级类别查询一级类别
       Category category = categoryService.selectParentCategory(id);
        map.put("categoryList",categoryList);
        map.put("bookList",bookList);
        map.put("totalPage",totalPage);
        map.put("pageNum", pageNum);
        map.put("category", category);
        return "forward:/main/book_list.jsp";
    }
    @RequestMapping("/selectOne")
    public String selectOne(Integer id,Map map) {
      Book book = bookService.selectOne(id);
      Category category = categoryService.selectCategoryName(book.getCid());
      List commentList = commentService.selectAllComment(id);
        map.put("book",book);
        map.put("category",category);
        map.put("commentList",commentList);
        return "forward:/main/product.jsp";
    }


}
